<template>
  <div class="user-define-container">
    <input
      type="file"
      id="uploadImage"
      ref="uploadImage"
      hidden="hidden"
      @change="handleUpload"
    />
    <div class="user-list">
      <div
        v-for="item in $store.state.userList"
        :key="item.id"
        class="user-item"
      >
        <div
          class="headImgContainer"
          @click="uploadImg(item.id)"
          v-show="item.headImg"
        >
          <img :src="item.headImg" v-if="item.headImg" />
        </div>
        <div
          class="blank-img"
          @click="uploadImg(item.id)"
          v-show="!item.headImg"
        >
          <i class="iconfont icon-jia1"></i>
        </div>
        <el-input
          v-model="item.name"
          style="width: 120px"
          :placeholder="item.name"
          ref="name"
          autofocus="true"
        ></el-input>
      </div>
    </div>
    <el-button id="add-user-button" type="primary" @click="addUser" 
      >添加用户</el-button
    >
  </div>
</template>
<script>
export default {
  data() {
    return {};
  },
  props: {
    msgData: {
      type: Object,
    },
  },
  methods: {
    addUser() {
      if (
        this.$store.state.dialogueType == "single" &&
        this.$store.state.userList.length == 2
      ) {
        this.$message({
          message: "单人会话模式下请勿添加多余用户！",
          showClose: true,
          type: "warning",
        });
        return;
      }
      this.$store.commit("addUser");
    },
    uploadImg(id) {
      this.$store.commit("EditUploadID", id);
      this.$refs["uploadImage"].click();
    },
    handleUpload(e) {
      let that = this;
      console.log("成功接收");
      let file = e.target.files[0];
      if (!file.type.match("image.*")) {
        alert("请选择图片类型的文件!");
        return;
      }
      var reader = new FileReader();
      reader.onload = function (e) {
        let url = e.target.result;
        that.$store.state.userList[that.$store.state.uploadID].headImg = url;
      };
      reader.readAsDataURL(file);
    },
  },
};
</script>
<style lang="scss" scoped>
$head-size: 120px;
.user-define-container {
  display: flex;
}
.user-list {
  display: flex;
  flex-grow: 1;
  position: relative;
  height: 200px;
  overflow: scroll;
  overflow-y: hidden;
  &::-webkit-scrollbar {
    width: 10px; /*高宽分别对应横竖滚动条的尺寸*/
    height: 12px;
  }
  &::-webkit-scrollbar-thumb {
    /*滚动条里面小方块*/
    border-radius: 10px;
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    background: #68c3ff;
  }
  &::-webkit-scrollbar-track {
    /*滚动条里面轨道*/
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0);
  }
  .user-item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 160px;
    flex-shrink: 0;
    margin: 5px;
    background-color: white;
    .headImgContainer {
      width: $head-size;
      height: $head-size;
      background-color: rgb(255, 255, 255);
      //   background-color: white;
      flex-shrink: 0;
      margin-top: 10px;
      margin-bottom: 10px;
      border-radius: 16px;
      img {
        object-fit: cover;
        width: $head-size;
        height: $head-size;
        border-radius: 12px;
        cursor: pointer;
      }
    }
    .user-name {
      line-height: 20px;
    }
  }
}
.blank-img {
  display: flex;
  justify-content: center;
  align-items: center;
  width: $head-size + (-2px);
  height: $head-size + (-2px);
  margin-top: 10px;
  margin-bottom: 10px;
  border-radius: 12px;
  border: 1px dashed #d9d9d9;
  flex-shrink: 0;
  &:hover {
    border-color: #409eff;
  }
  cursor: pointer;
  i {
    font-size: $head-size/2;
    color: rgb(200, 200, 200);
  }
}
#add-user-button {
  width: 120px;
  height:60px;
  flex-shrink: 0;
}
.el-input__inner {
  text-align: center;
}
</style>